//
//  HHContactUsViewController.m
//  HHBroker
//
//  Created by He Wei on 9/15/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHContactUsViewController.h"
#import "HHContactUsInfoModel.h"
#import "HHContactUsInfoCell.h"
#import "UIView+callingAlertview.h"

// 联系我们
#define KUrlContactUs  @"http://api2.jiayuan.com/vip/broker/contactus.php"
#define kTagOfPhoneAlertView 12345
@interface HHContactUsViewController () <UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate>
@property (nonatomic, copy) NSString *phoneNumber;
@end

@implementation HHContactUsViewController

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Life Cycle


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"联系我们";
    [self prepareData];
    [self.view addSubview:self.tableView];
}

#pragma mark -
#pragma mark - Delegate

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseCell = @"reuseCell";
    HHContactUsInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseCell];
    if (cell == nil) {
        cell = [[HHContactUsInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseCell];
    }
    cell.model = self.dataArray[indexPath.row];
    //HHContactUsInfoModel *model = self.dataArray[indexPath.row];
    //cell.textLabel.text = cell.model.title;
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.index = indexPath;
    HHContactUsInfoModel *model = self.dataArray[indexPath.row];
    NSInteger acton = [model.action integerValue];
    switch (acton) {
        case 100:
        case 101:
        {
            NSString *des = model.des;
            if (!des || des.length == 0) {
                [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
                return;
            }
            self.phoneNumber = [NSString getRealNumber:des];
            UIView *view = [UIView callAphoneNumber:des withTitle:nil];
            if ([view isKindOfClass:[UIAlertView class]]) {
                UIAlertView *alertView = (UIAlertView *)view;
                alertView.tag = kTagOfPhoneAlertView;
                alertView.delegate = self;
                [alertView show];
            }
            else {
                [self.view addSubview:view];
            }
        }
            break;
        case 102:
        {
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            if (!model.des) {
                [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
                return;
            }
            pasteboard.string = model.des;
            MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hub.mode = MBProgressHUDModeText;
            hub.labelText = @"复制微信公众账号成功";
            hub.completionBlock = ^{
                [tableView deselectRowAtIndexPath:indexPath animated:YES];
            };
            [hub hide:YES afterDelay:1];
        }
            break;
        case 103:
        {
            
        
        }
        default:
            break;
    }

}



- (void)prepareData
{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"ContactUsInfoList" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    for (NSDictionary *dic in array) {
        HHContactUsInfoModel *model = [[HHContactUsInfoModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.dataArray addObject:model];
    }
    [HttpRequestManager getWithUrlString:KUrlContactUs success:^(id response){
        NSInteger code = [[response objectForKey:@"code"] integerValue];
        if (code == 1) {
            NSDictionary *dic = [response objectForKey:@"data"];
            NSArray *keyArray = @[@"service_hotline_phone", @"complaint_phone", @"weixin_number"];
            for (int i=0; i<keyArray.count; i++) {
                NSString *key = keyArray[i];
                HHContactUsInfoModel *model = self.dataArray[i];
                model.des = [dic objectForKey:key];
            }
            [self.tableView reloadData];
        }
        
    } failure:^(NSError *error){
    }];
    
    
    
    
     //?????
    
    
    
    
    
    
    
    
    
    
    
}
@end
